(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.apTradepartner.deleteRejectController', deleteRejectController);
	deleteRejectController.$inject = [ '$scope', 'Constants', 'modalService', 'layoutConfig', 'validatorConfig', 'gridService', 'authService', 'apTradepartnerService', '$filter' ];
	function deleteRejectController($scope, Constants, modalService, layoutConfig, validatorConfig, gridService, authService, apTradepartnerService, $filter) {
		var ctrl = this, apTradepartnerInfo = $scope.$stateParams.params;
		//ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.apTradepartner = {};

		ctrl.init = function() {
			var params = {
					taskId : apTradepartnerInfo.taskId
			};
			// init acct
			apTradepartnerService.initProfAcct(layoutConfig, $scope, ctrl);
			// init int
			//apTradepartnerService.initProfInt(layoutConfig, $scope, ctrl);
			//apTradepartnerService.initProfCostOfFundInt(layoutConfig, $scope, ctrl);
			// init pastdue
			apTradepartnerService.initProfPastdue(layoutConfig, $scope, ctrl);
			// init fee
			apTradepartnerService.initProfFee(layoutConfig, $scope, ctrl);
			// init doc
			apTradepartnerService.initEventDoccfg(layoutConfig, $scope, ctrl);
			// init limit facility
			//apTradepartnerService.initProfLimitFaci(layoutConfig, $scope, ctrl);
			//apTradepartnerService.initProfLimitInsFaci(layoutConfig, $scope, ctrl);
			//apTradepartnerService.initProfLimitPurFaci(layoutConfig, $scope, ctrl);
			// init Settlement Instruction
			apTradepartnerService.initProfSetlinst(layoutConfig, $scope, ctrl);
			// init cust prop define
			apTradepartnerService.initCustProp(layoutConfig, $scope, ctrl);
			//
			apTradepartnerService.initGeneral($scope, ctrl).then(function() {
				authService.deleteReject(params).then(function(response) {
					var initApTradepartner = response.data.model;
					ctrl.action = "deleteReject";
					apTradepartnerService.initLayoutConfigAndModel($scope, ctrl, initApTradepartner, ctrl.action, false).then(function() {
						// ctrl.apTradepartner = initApTradepartner; done in initLayoutConfig
						ctrl.apTradepartner.taskId = response.data.task.taskId || apTradepartnerInfo.taskId;
					});
				});
			});
		};
		
		ctrl.deleteRejectSubmit = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.delete', args : [$filter("encode")(ctrl.apTradepartner.sellerCode)]} ],
				'btnCallback' : {
					'ok' : function(response) {
						authService.deleteRejectSubmit(ctrl.apTradepartner).then(deleteSuccessful, deleteFailed);
					}
				}
			});
		};
		
		ctrl.deleteRejectCancel = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.cancel', args : [$filter("encode")(ctrl.apTradepartner.sellerCode)]} ],
				'btnCallback' : {
					'ok' : function(response) {
						authService.deleteRejectCancel(ctrl.apTradepartner).then(cancelSuccessful, cancelFailed);
					}
				}
			});
		};
		
		function deleteSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.delete' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.aptradepartner.list',
					'state' : 'cda.apTradepartner.list',
					'params' : {}
				}]
			});
		}

		function deleteFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		function cancelSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.cancel' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.aptradepartner.list',
					'state' : 'cda.apTradepartner.list',
					'params' : {}
				}]
			});
		}

		function cancelFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
	}
})(angular);